﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using OL.FrameCore.Application;
using OL.FrameCore.Application.Role;
using OL.FrameCore.Application.Role.Dto;
using OL.FrameCore.WebUI.ViewModels.Role;

namespace OL.FrameCore.WebUI.Controllers
{
    public class RoleController : ControllerBase
    {
        IRoleService _roleService;

        public RoleController(IRoleService roleService)
        {
            _roleService = roleService;
        }

        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public IActionResult GetRoleList(GetRoleListViewRequest request)
        {
            var serviceRequest = ToMap<GetRoleListViewRequest, GetRoleListRequest>(request);
            serviceRequest.PageIndex = request.page - 1;
            serviceRequest.PageSize = request.limit;
            var serviceResult = _roleService.GetRoleList(serviceRequest);
            var result = new
            {
                code = 0,
                msg = "",
                count = serviceResult.ItemsCount,
                data = serviceResult.Items
            };
            return Json(result);
        }

        /// <summary>
        /// 添加或修改角色页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult AddOrUpdate(int? id)
        {
            GetRoleResponse model = new GetRoleResponse();
            if (id != null)
            {
                model = _roleService.GetRole(id.Value);
            }
            else
            {
                model = _roleService.GetEmptyRole();
            }
            return View(model);
        }

        /// <summary>
        /// 提交添加或修改角色
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostAddOrUpdateRole(PostAddOrUpdateViewRequest request)
        {
            //验证模型数据
            if (IsNotValidModel)
            {
                return FailResult(ModelErrorMessage);
            }

            if (request.Id == null || request.Id == 0)//添加
            {
                var serviceRequest = ToMap<PostAddOrUpdateViewRequest, AddRoleRequest>(request);
                serviceRequest.OpeatorId = UserId;
                var result = _roleService.AddRole(serviceRequest);
                return Json(result);
            }
            else//修改
            {
                var serviceRequest = ToMap<PostAddOrUpdateViewRequest, UpdateRoleRequest>(request);
                serviceRequest.OpeatorId = UserId;
                var result = _roleService.UpdateRole(serviceRequest);
                return Json(result);
            }
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteRole(int id)
        {
            var result = _roleService.DeleteRole(id, UserId);
            return Json(result);
        }
    }
}